<?php
/**
 * Created by PhpStorm.
 * User: Monkey
 * Date: 2021/10/13
 * Time: 11:27
 * Desc: 会员
 */
declare (strict_types=1);

namespace app\api\controller;

use app\api\base\controller\Base;
use app\api\validate\UserValidate;
use app\extend\model\UserAuth;

class User extends Base
{
    /**
     * 获取用户信息
     */
    public function getInfo()
    {
        //持仓金额
        $ccje = \app\extend\model\Order::where([
            ['uid','=',self::$UserInfo['id']],
            ['buytime','between',[date('Y-m-d'),date('Y-m-d 23:59:59')]],
        ])->sum('fee');

        //总盈亏
        $yingkui = \app\extend\model\Order::where([
            ['uid','=',self::$UserInfo['id']],
            ['status','=',1],
            ['buytime','between',[date('Y-m-d'),date('Y-m-d 23:59:59')]],
        ])->sum('ploss');

        self::$UserInfo['ccje'] = $ccje;
        self::$UserInfo['yingkui'] = $yingkui;
        return json(_S('获取成功',self::$UserInfo));
    }

    /**
     * 修改登录密码
     */
    public function editPassword()
    {
        if (request()->isPost()) {
            $validate = new UserValidate();
            $ret = $validate->scene('editPassword')->check(input());
            if(!$ret){
                return json(_E($validate->getError()));
            }

            $oldPassword = input('old_password', '');
            $newPassword = input('new_password', '');

            $userInfo = \app\extend\model\User::where([
                ['id','=',self::$UserInfo['id']]
            ])->field('id,password,salt')->find();

            if($userInfo->password != password($oldPassword, $userInfo->salt)){
                return json(_E('原登录密码错误'));
            }

            $pwdArr = password($newPassword);

            $result = \app\extend\model\User::where([
                ['id','=',self::$UserInfo['id']]
            ])->update([
                'password' => $pwdArr['password'],
                'salt' => $pwdArr['salt'],
                'updated_at' => date('Y-m-d H:i:s')
            ]);

            if(!$result){
                return json(_E('更新失败'));
            } else {
                return json(_S('更新成功'));
            }
        }
    }

    /**
     * 修改支付密码
     */
    public function editPayPassword()
    {
        if (request()->isPost()) {
            $validate = new UserValidate();
            $ret = $validate->scene('editPayPassword')->check(input());
            if(!$ret){
                return json(_E($validate->getError()));
            }

            $oldPassword = input('old_password', '');
            $newPassword = input('new_password', '');

            $userInfo = \app\extend\model\User::where([
                ['id','=',self::$UserInfo['id']]
            ])->field('id,pay_password,pay_salt')->find();

            if($userInfo->pay_password != password($oldPassword, $userInfo->pay_salt)){
                return json(_E('原支付密码错误'));
            }

            $pwdArr = password($newPassword);

            $result = \app\extend\model\User::where([
                ['id','=',self::$UserInfo['id']]
            ])->update([
                'pay_password' => $pwdArr['password'],
                'pay_salt' => $pwdArr['salt'],
                'updated_at' => date('Y-m-d H:i:s')
            ]);

            if(!$result){
                return json(_E('更新失败'));
            } else {
                return json(_S('更新成功'));
            }
        }
    }

    /**
     * 实名认证
     */
    public function auth()
    {
        if (request()->isPost()) {
            $validate = new UserValidate();
            $ret = $validate->scene('auth')->check(input());
            if(!$ret){
                return json(_E($validate->getError()));
            }

            $realName = input('real_name', '');
            $idCard = input('id_card', '');
            $zmImg = input('zm_img', '');
            $fmImg = input('fm_img', '');

            $saveData = [
                'real_name' => $realName,
                'id_card' => $idCard,
                'zm_img' => $zmImg,
                'fm_img' => $fmImg,
                'status' => 0
            ];

            $userAuthInfo = UserAuth::where(['uid' => self::$UserInfo['id']])->find();

            if(!empty($userAuthInfo)){
                $saveData['updated_at'] = date('Y-m-d H:i:s');

                $result = UserAuth::where([
                    ['uid','=',self::$UserInfo['id']]
                ])->update($saveData);
            } else {
                $saveData['created_at'] = date('Y-m-d H:i:s');
                $saveData['uid'] = self::$UserInfo['id'];

                $result = UserAuth::insert($saveData);
            }

            if(!$result){
                return json(_E('更新失败'));
            } else {
                return json(_S('更新成功'));
            }
        } else {
            $userAuthInfo = UserAuth::where([
                ['uid','=',self::$UserInfo['id']]
            ])->find();

            return json(_S('获取成功',$userAuthInfo));
        }
    }
}
